import multiprocessing

# 定义示例函数，带有多个参数
def example_function(n,a, b, c):
    import time
    time.sleep(n)
    print("Function executed:", a, b, c)

# 设定超时时间
def run_with_timeout(func, args, timeout):
    # 创建子进程
    process = multiprocessing.Process(target=func, args=args)
    process.start()
    # 等待超时时间
    process.join(timeout)#子进程阻塞主进程，时长为timeout。
    # 如果进程仍在运行，则超时，终止进程
    if process.is_alive():#如何达到了最大阻塞时长，进程仍然活跃。
        process.terminate()#
        # 触发超时异常
        raise TimeoutError("Function execution timed out")
if __name__ == '__main__':
    # 测试
    try:
        run_with_timeout(example_function, (1000000,10, 20, 30), 5)  # 设置超时时间为 5 秒，参数为 10, 20, 30
    except TimeoutError as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Function execution timed out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
